package pl.koziolekweb.translator.gui.controllers;

import javax.swing.JComboBox;
import javax.swing.JTextArea;

import org.apache.log4j.Logger;

import pl.koziolekweb.translator.engine.Language;
import pl.koziolekweb.translator.engine.TranslatorService;
import pl.koziolekweb.translator.utilites.Context;

public class TranslatorController extends AbstractController {

	private final JComboBox languageListFrom;

	private final JComboBox languageListTo;
	private final JTextArea textFrom;
	private final JTextArea textTo;
	private final TranslatorService translatorService;

	private Logger logger;

	public TranslatorController(final Context context) {
		logger = (Logger) context.getObject("logger");
		languageListFrom = (JComboBox) context.getObject("languageListFrom");
		languageListTo = (JComboBox) context.getObject("languageListTo");
		textFrom = (JTextArea) context.getObject("textFrom");
		textTo = (JTextArea) context.getObject("textTo");
		translatorService = (TranslatorService) context
				.getObject("translatorService");
		super.setContext(context);
	}

	@Override
	public void procced(final Context context) {
		Thread thread = new Thread() {
			@Override
			public void run() {
				try {
					String result = translatorService.translate(textFrom
							.getText(), (Language) languageListFrom
							.getSelectedItem(), (Language) languageListTo
							.getSelectedItem());
					textTo.setText(result);
					logger.info("Text:\n" + textFrom.getText() + "\nForm:\n"
							+ languageListFrom.getSelectedItem() + "\nTo:\n"
							+ languageListTo.getSelectedItem()
							+ "\nResult is:\n" + result);
				} catch (Throwable e1) {
					e1.printStackTrace();
				}
			}
		};
		thread.start();
	}

}
